package com.zym.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zym.baidu.TransApi;
import com.zym.domain.*;
import com.zym.service.IFyService;
import io.itit.itf.okhttp.FastHttpClient;
import org.springframework.stereotype.Service;

/**
 * @author ZouYangMing
 */
@Service
public class FyServiceImpl implements IFyService {

    /**
     * 百度翻译
     * @return
     */
    @Override
    public BdFy bdfy(String text) {
        TransApi BaiduApi = new TransApi("20220511001212483", "hv2r2AGgxVHNFosxED2a");
        String jsonStr = JSON.parseObject(BaiduApi.getTransResult(text, "auto", "zh")).getString("trans_result");
        BdFy bdFy = JSON.parseObject(jsonStr.substring(1, jsonStr.length() - 1), BdFy.class);
        return bdFy;
    }

    /**
     * 腾讯翻译
     * @return
     */
    @Override
    public TxFy txfy(String text) {
        String str = "";
        try {
            str = FastHttpClient.post()
                    .addParams("source","auto")
                    .addParams("target","cn")
                    .addParams("sourceText",text)
                    .addParams("qtv","758ef41584436944")
                    .addParams("qtk","6nvdEWpYVLpLIn1lMb6ke%2BXb0klOm9W3zLO7AdQK9JsiGcyu1eSUI6U8cUGUKfRblJQ3B6zIc3y6%2BahQ%2BVesvVY0wpWHjfh1S8HcwwQf7cqOngBMIGEVGznM3PgX17R%2Bh61y175eNHsxWWFEV1tdHg%3D%3D")
                    .addParams("ticket","")
                    .addParams("randstr","")
                    .addParams("sessionUuid","translate_uuid1659091347710")
                    .url("https://fanyi.qq.com/api/translate")
                    .build()
                    .execute()
                    .string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            String jsonStr = JSON.parseObject(str).getJSONObject("translate").get("records").toString();
            if(jsonStr == null){
                return new TxFy();
            }
            TxFy txFy = JSON.parseObject(jsonStr.substring(1, jsonStr.length() - 1), TxFy.class);
            return txFy;
        }
    }

    /**
     * 搜狗翻译
     * @return
     */
    @Override
    public SgFy sgfy(String text) {
        String response = "";
        SgFy sgFy;
        try {
            response = FastHttpClient.post()
                    .addParams("from","auto")
                    .addParams("to","zh-CHS")
                    .addParams("client","web")
                    .addParams("text",text)
                    .addParams("uuid","55c32304-08c2-4a67-b277-1e2d059c961b")
                    .addParams("pid","sogou-dict-vr")
                    .addParams("addSugg","on")
                    .url("https://fanyi.sogou.com/reventondc/suggV3")
                    .build()
                    .execute()
                    .string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            String jsonStr = JSON.parseObject(response).getString("sugg");
            if(jsonStr == null){
                return new SgFy();
            }
            sgFy = JSON.parseObject(jsonStr.substring(1, jsonStr.length() - 1), SgFy.class);
            if(sgFy != null){
                String sgfy_v = sgFy.getV().substring(2);
                String[] sgfy_v_arr = sgfy_v.split("；");
                sgFy.setV(sgfy_v_arr[0]);
            }
        }
        return sgFy;
    }

    /**
     * 金山翻译
     * @return
     */
    @Override
    public JsFy jsfy(String text) {
        String url = "https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_web_fanyi&sign=eac5e9c9b40b58a2&from=en&to=auto&q=" + text;
        String response = "";
        JsFy jsFy;
        try {
            response = FastHttpClient.post().url(url).build().execute().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JSONObject json = JSON.parseObject(response);
            jsFy = JSON.parseObject(json.getString("content"), JsFy.class);
            if(jsFy != null){
                jsFy.setText(text);
            }
        }
        return jsFy;
    }

    /**
     * 360翻译
     * @return
     */
    @Override
    public Name360Fy name360fy(String text) {
        String key = text;
        String url = "https://fanyi.so.com/index/search?eng=1&validate=&ignore_trans=0&query=" + key;
        String response = null;
        Name360Fy name360Fy;
        try {
            response = FastHttpClient.post().addHeader("pro","fanyi").url(url).build().execute().string();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JSONObject json = JSON.parseObject(response).getJSONObject("data");
            name360Fy = JSON.parseObject(json.toJSONString(), Name360Fy.class);
            if(name360Fy != null){
                name360Fy.setWord(key);
            }
        }
        return name360Fy;
    }
}
